﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace POSPlus.DatabaseOperations
{
	public class PrintManagerSign : DatabaseObject
	{
		public String Upc { get; set; }
		public String JobName { get; set; }
		public String BatchNo { get; set; }
		public Int32 Copies { get; set; }
		public string Type { get; set; }

		public PrintManagerSign(String connectionString)
		{
			ConnectionString = connectionString;
			PopulateDefault();
		}

		public PrintManagerSign(String connectionString, String upc, String batchNo)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, JobName, Copies, Type FROM " +
					"POSplus_PrintManagerDisplays WHERE Upc = @Upc AND BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						Upc = FromDB<String>(reader["Upc"]);
						JobName = FromDB<String>(reader["JobName"]);
						Copies = FromDB<Int32>(reader["Copies"]);
						Type = FromDB<string>(reader["Type"]);
						BatchNo = FromDB<string>(reader["BatchNo"]);
						reader.Close();
					}
				}
				connection.Close();
			}
		}

		protected override void PopulateDefault()
		{
			Upc = "";
			JobName = "";
			Copies = 0;
			Type = "";
			BatchNo = "";
		}

		public override Boolean Create()
		{
			if (string.IsNullOrEmpty(JobName))
				throw new ArgumentNullException("JobName", "JobName cannot be empty.");

			int rowsAffected = 0;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("INSERT INTO POSplus_PrintManagerDisplays " +
					"(Upc, BatchNo, JobName, Copies, Type, LocAisle, LocBlock, LocSeq) " +
					"VALUES (@Upc, @BatchNo, @JobName, @Copies, @Type, @LocAisle, @LocBlock, @LocSeq)", connection))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@Copies", Copies);
					command.Parameters.AddWithValue("@Type", Type);
					command.Parameters.AddWithValue("@LocAisle", 0);
					command.Parameters.AddWithValue("@LocBlock", 0);
					command.Parameters.AddWithValue("@LocSeq", 0);
					command.Parameters.AddWithValue("@BatchNo", BatchNo);
					try
					{
						rowsAffected = command.ExecuteNonQuery();
					}
					catch (SqlException e)
					{
						string errorMessage;

						switch (e.Class)
						{
							case 14: 
								errorMessage = "There is already a sign with that Upc remove the " +
									"old sign before creating a new one.";
								break;
							default: errorMessage = e.Message;
								break;
						}

						MessageBox.Show(errorMessage, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
				connection.Close();
			}
			return rowsAffected > 0;
		}

		public override Boolean Update()
		{
			if (string.IsNullOrEmpty(JobName))
				throw new ArgumentNullException("JobName", "JobName cannot be empty.");

			int rowsAffected = 0;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("UPDATE POSplus_PrintManagerDisplays SET " +
					"Upc = @Upc, JobName = @JobName, Copies = @Copies, Type = @Type, BatchNo = @BatchNo WHERE Upc = @Upc", connection))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@Copies", Copies);
					command.Parameters.AddWithValue("@Type", Type);
					command.Parameters.AddWithValue("@BatchNo", BatchNo);
					rowsAffected = command.ExecuteNonQuery();
				}
				connection.Close();
			}
			return rowsAffected > 0;
		}

		public override Boolean Delete()
		{
			int rowsAffected = 0;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("DELETE FROM POSplus_PrintManagerDisplays " +
					"WHERE Upc = @Upc AND JobName = @JobName AND BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@BatchNo", BatchNo);
					rowsAffected = command.ExecuteNonQuery();
				}
				connection.Close();

				PopulateDefault();
			}
			return rowsAffected > 0;
		}

		public static IEnumerable<PrintManagerSign> GetByJobName(string connectionString, string jobName)
		{
			List<PrintManagerSign> signs = new List<PrintManagerSign>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT JobName, Copies, Upc, Type FROM " +
					"POSplus_PrintManagerDisplays WHERE JobName = @JobName ORDER BY JobName, Upc",
					connection))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							PrintManagerSign sign = new PrintManagerSign(connectionString);
							sign.Upc = FromDB<String>(reader["Upc"]);
							sign.JobName = FromDB<String>(reader["JobName"]);
							sign.Copies = FromDB<Int32>(reader["Copies"]);
							sign.Type = FromDB<string>(reader["Type"]);
							sign.BatchNo = FromDB<string>(reader["BatchNo"]);
							signs.Add(sign);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return signs;
		}
	}
}
